package org.dromara.timemate.controller;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.ValidatorUtils;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.QueryGroup;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.timemate.domain.bo.DataIndexBo;
import org.dromara.timemate.domain.bo.DeviceBo;
import org.dromara.timemate.domain.vo.DataIndexVo;
import org.dromara.timemate.domain.vo.DeviceVo;
import org.dromara.timemate.service.HomeService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;

import java.util.Arrays;


/**
 * app首页接口入口
 */

@Validated
@RestController
@RequestMapping("/tm/home")
public class HomeController extends BaseController {

    @Resource
    private HomeService homeService;

    /**
     * 获取设备列表
     *
     * @param deviceType 设备类型:camera-摄像头;watch-手表;wristband-手环;mattress-床垫
     * @param deviceName 设备名称
     * @param pageQuery 分页对象
     * @return {@link TableDataInfo}
     */
    @GetMapping("/listDevices")
    public TableDataInfo<DeviceVo> listDevices(@RequestParam(value = "deviceType",required = false) String deviceType, @RequestParam(value = "deviceName",required = false) String deviceName, PageQuery pageQuery) {
        return homeService.queryPageDeviceList(deviceType, deviceName, pageQuery);
    }

    /**
     * 添加设备信息
     * @param deviceBo：userId（必填）、wearerId、deviceType（必填）、deviceName
     * @return
     */
    @PostMapping("/device/add")
    public R<Void> addDeviceManageInfo(@Valid @RequestBody DeviceBo deviceBo){
        ValidatorUtils.validate(deviceBo, AddGroup.class);
        return toAjax(homeService.addDeviceInfo(deviceBo));
    }

    /**
     * 通过上传设备id，单个删除
     * @param id
     * @return
     */
    @DeleteMapping("/device/{id}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] id) {
        return toAjax(homeService.deleteWithValidByIds(Arrays.asList(id)));
    }

    /**
     *通过穿戴者id、设备名称、设备类型进行查询
     * @param bo
     * @param pageQuery
     * @return
     */
    @GetMapping("/listDataIndex")
    public TableDataInfo<DataIndexVo> list(@Validated(QueryGroup.class) DataIndexBo bo, PageQuery pageQuery) {
        return  homeService.queryPageDataIndexList(bo, pageQuery);
    }
}
